/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.nanosn.coffeemaker.application.commands;

import com.nanosn.coffeemaker.Project;
import com.nanosn.util.parsers.commandline.Command;
import java.io.File;
import java.util.ArrayList;
import java.util.List;
import org.codehaus.groovy.tools.shell.Groovysh;
import org.codehaus.groovy.tools.shell.util.NoExitSecurityManager;

/**
 *
 * @author selkhateeb
 */
public class Shell implements Command {

    public void run(String[] args) throws Exception {


        File dir = new File(Project.PWD);
//        IO io = new IO();

//        List<String> files = getAllFilesIncludingTheOnesInSubdirectories(dir);

        // Boot up the shell... :-)
        Groovysh shell = new Groovysh();
//        Groovysh shell = new Groovysh(io);

//        for (String file : files) {
//            if (file.contains(File.separator + "models" + File.separator)) {
//                System.out.println("loading " + file);
//                shell.execute("load " + file);
//            }
//        }

        SecurityManager psm = System.getSecurityManager();
        System.setSecurityManager(new NoExitSecurityManager());

        try {

            shell.run();
        }
        finally {
            System.setSecurityManager(psm);
        }
    }

    public String getHelp() {
        return "Starts Groovy's interactive shell";
    }

    private static List<String> getAllFilesIncludingTheOnesInSubdirectories(File dir) {

        ArrayList<String> files = new ArrayList<String>();
        for (File file : dir.listFiles()) {
            if (file.isDirectory()) {
                files.addAll(getAllFilesIncludingTheOnesInSubdirectories(file));
            }
            else {
                files.add(file.getAbsolutePath());
            }
        }
        return files;
    }
}
